﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///AttachFile 的摘要说明
/// </summary>
public partial class AttachFile
{
    public static AttachFile Get(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.AttachFile.FirstOrDefault(row => row.ID == id);
        }
    }

    public static List<AttachFile> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.AttachFile.OrderByDescending(row => row.ID).ToList();
        }
    }

    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.AttachFile.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<AttachFile>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.AttachFile.FirstOrDefault(row => row.ID == id);
            if (objItem == null)
                return;
            context.AttachFile.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }

}